//
//  YZMSegmentViewScroll.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/23.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSegmentViewScroll.h"
#import <Masonry/Masonry.h>
#import "YZMAppStyle.h"

#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

static const float segment_view_margin_for_red_line = 80;

@interface YZMSegmentViewScroll()<UIScrollViewDelegate>

@end


@implementation YZMSegmentViewScroll

#pragma mark  -- Public Method

/*设置标题列表*/
- (void)setTitleArray:(NSArray *)titleArray{
    
    [_titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_buttonArray removeAllObjects];
    
    _titleArray = titleArray;
    
    if (!titleArray || titleArray.count == 0) {
        
        return;
    }
    
    NSUInteger titleCount = [titleArray count];
    UIButton *lastObject = nil;
    
    for (NSString *title in titleArray) {
        
        UIButton *button = [UIButton new];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForMainTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForIconColor] forState:UIControlStateSelected];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        button.tag = [titleArray indexOfObject:title];
        [button addTarget:self action:@selector(clickForSwitchView:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make){
            
            make.top.height.equalTo(_titleView);
            make.width.mas_equalTo(ScreenWidth / titleCount);
            
            if (lastObject) {
                
                make.left.mas_equalTo(lastObject.mas_right);
                if (_titleArray.count == 2) {
                    //分割线
                    UIView *lineView = [YZMAppStyle splitLine];
                    [_titleView addSubview:lineView];
                    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        
                        make.centerX.centerY.equalTo(_titleView);
                        make.height.equalTo(button);
                        make.width.mas_equalTo(0.5);
                    }];
                }
                
            }else{
                make.left.mas_equalTo(_titleView);
            }
            
        }];
        
        
        if (button.tag == 0) {
            
            button.selected = YES;
            
            _redLineView = [UIView new];
            _redLineView.backgroundColor = [YZMAppStyle colorForIconColor];
            [self addSubview:_redLineView];
            [_redLineView mas_makeConstraints:^(MASConstraintMaker *make){
                
                make.width.mas_equalTo(button.mas_width).offset(-segment_view_margin_for_red_line);
                make.left.mas_equalTo(segment_view_margin_for_red_line/2);
                make.centerY.mas_equalTo(button.mas_bottom);
                make.height.mas_equalTo(3);
            }];
        }
        
        [_buttonArray addObject:button];
        lastObject = button;
    }
    
    [_containerView mas_remakeConstraints:^(MASConstraintMaker *make){
        
        make.edges.equalTo(_contentSV);
        make.width.equalTo(_titleView).multipliedBy(_buttonArray.count);
    }];
}

/*设置滑动的页面视图列表*/
- (void)setViewArray:(NSArray *)viewArray{
    
    if (!_titleArray || !viewArray || _titleArray.count != viewArray.count) {
        
#ifdef DEBUG
        NSAssert(0, @"视图个数与 标题个数不符。");
#endif
        return;
    }
    
    _viewArray = viewArray;
    
    UIView *lastView = nil;
    UIView *firstView = nil;
    
    for (UIView *view in _viewArray) {
        
        NSUInteger index = [_viewArray indexOfObject:view];
        
        float height_for_view = view.frame.size.height;
        
        if (height_for_view <= 0.000001) {
            
            height_for_view = 1;
        }
        
        if (index == 0) {
            
            firstView = view;
        }
        
        view.backgroundColor = [YZMAppStyle colorForSplitLine];
        [_containerView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make){
            
            make.top.mas_equalTo(_containerView);
            make.height.mas_equalTo(height_for_view);
            make.width.mas_equalTo(self);
            if (lastView) {
                
                make.left.mas_equalTo(lastView.mas_right);
            }else{
                
                make.left.mas_equalTo(0);
            }
        }];
        
        lastView = view;
    }
    
    UILabel *label = [YZMAppStyle labelWithMediumGray];
//    label.text = @"加载中...";
    label.textAlignment = NSTextAlignmentCenter;
    [_contentSV insertSubview:label atIndex:0];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_containerView);
        make.centerX.equalTo(self);
    }];
    
    [_containerView mas_updateConstraints:^(MASConstraintMaker *make){
        
        make.height.greaterThanOrEqualTo(firstView);
        make.height.mas_greaterThanOrEqualTo(80);
    }];
}


#pragma mark -- Private Method

- (void)addConstraints{
    
    WS(ws)
    
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.left.width.equalTo(ws);
        make.height.mas_equalTo(40);
    }];
    
    UIView *line = [YZMAppStyle splitLine];
    [self addSubview:line];
    [self insertSubview:line aboveSubview:_titleView];
    [line mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.left.width.equalTo(_titleView);
        make.centerY.mas_equalTo(_titleView.mas_bottom);
    }];
    
    [_contentSV mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.mas_equalTo(line.mas_bottom);
        make.left.width.equalTo(ws);
        make.height.mas_equalTo(_containerView);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.bottom.equalTo(_contentSV);
    }];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_redLineView mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.left.mas_equalTo(scrollView.contentOffset.x/scrollView.contentSize.width * scrollView.frame.size.width +segment_view_margin_for_red_line/2);
    }];
}

@end
